package com.lexue.articlesync.repository;

import com.lexue.articlesync.entity.PublishConfig;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 发布配置数据访问接口
 */
@Repository
public interface PublishConfigRepository extends JpaRepository<PublishConfig, Long> {
    
    /**
     * 根据文章ID查找发布配置
     */
    List<PublishConfig> findByArticleId(Long articleId);
    
    /**
     * 根据视频ID查找发布配置
     */
    List<PublishConfig> findByVideoId(Long videoId);
    
    /**
     * 根据平台ID查找发布配置
     */
    List<PublishConfig> findByPlatformId(Long platformId);
    
    /**
     * 根据状态查找发布配置
     */
    List<PublishConfig> findByStatus(PublishConfig.PublishStatus status);
    
    /**
     * 查找启用的发布配置
     */
    List<PublishConfig> findByEnabledTrue();
    
    /**
     * 根据文章和平台查找配置
     */
    @Query("SELECT pc FROM PublishConfig pc WHERE pc.article.id = :articleId AND pc.platform.id = :platformId")
    PublishConfig findByArticleIdAndPlatformId(@Param("articleId") Long articleId, @Param("platformId") Long platformId);
    
    /**
     * 根据视频和平台查找配置
     */
    @Query("SELECT pc FROM PublishConfig pc WHERE pc.video.id = :videoId AND pc.platform.id = :platformId")
    PublishConfig findByVideoIdAndPlatformId(@Param("videoId") Long videoId, @Param("platformId") Long platformId);
    
    /**
     * 查找需要重试的发布配置
     */
    @Query("SELECT pc FROM PublishConfig pc WHERE pc.status = 'FAILED' AND pc.retryCount < pc.maxRetryCount")
    List<PublishConfig> findFailedConfigsForRetry();
}